/*****************************************************************************

File:   otkCombStateUtils.java

Date          Build     Author        Version    Comments
18-Sep-12    P-20-14    dshah          $$1        Created
30-Oct-12    P-20-16    dshah          $$2        Updated
20-mar-15 P-30-05 gshmelev $$3  called GetCurrentSession

****************************************************************************/

/* 
   
Utilites for Combined state.

*/

package com.ptc.otkjavaexamples;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcCommand.*;
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcSolid.*;
import javax.swing.JOptionPane;
import java.io.*;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcFamily.*;
import com.ptc.pfc.pfcServer.*;
import com.ptc.pfc.pfcWindow.*;
import com.ptc.wfc.wfcSolid.*;
import com.ptc.wfc.wfcFeatureInstructions.*;
import com.ptc.pfc.pfcSelect.*;
import com.ptc.wfc.wfcSession.*;
import com.ptc.pfc.pfcUI.*;
import com.ptc.pfc.pfcExceptions.*;
import com.ptc.pfc.pfcXSection.*;
import com.ptc.pfc.pfcObject.*;
import com.ptc.wfc.wfcCombState.*;
import com.ptc.wfc.wfcXSection.*;
import com.ptc.wfc.wfcLayerState.*;
import com.ptc.pfc.pfcLayer.*;
import com.ptc.wfc.wfcModelItem.*; 
import com.ptc.pfc.pfcSimpRep.*;
import com.ptc.wfc.wfcAssembly.*;
import com.ptc.pfc.pfcBase.*; 

public class otkCombStateUtils
{
    
    public otkCombStateUtils()
    {
    }
    
    // Utility to return combined state from input solid.
    // Pass CombStateNum = -1 to select layer state through UI
    //*********************************************
	public static CombState otkUtilSelectCombinedState (WSolid WSolid, 
							    int CombStateNum)
    {
	try
	    {	
		Session session = pfcSession.GetCurrentSession ();
		
		CombStates WCombStates =   WSolid.ListCombStates();
		if(WCombStates == null)
		    return null;
		
		int CombStatesArraySize = WCombStates.getarraysize ();
		if(CombStatesArraySize > 0)
		    {
			if(CombStateNum != -1 &&(CombStateNum > 0&& CombStateNum <= CombStatesArraySize))
			    {
				CombState WCombState = WCombStates.get(CombStateNum -1);
				return (WCombState);
			    }
			// Select from UI
			else
			    {
				String line;
				String buff;
				stringseq msgs = stringseq.create();
				
				line = "Layer States: ";
				for (int i = 0; i < CombStatesArraySize; i++)
				    {
					CombState WCombState = WCombStates.get(i);
					if(WCombState != null)
					    {
						if(i+1 < CombStatesArraySize)
						    buff = " " + (i+1)+": "+WCombState.GetName();
						else
						    buff = " " + (i+1)+": "+WCombState.GetName();
						
						line += buff;
					    }
				    }
				msgs.append(line);
				session.UIDisplayMessage("combstate_msg.txt", "USER Enter number to select %0s", msgs);
				Integer SelectedCombStateNo = session.UIReadIntMessage(1, CombStatesArraySize);
				if(SelectedCombStateNo.intValue() > 0 && SelectedCombStateNo.intValue() <= CombStatesArraySize)
				    {
					CombState WCombState = WCombStates.get(SelectedCombStateNo.intValue()-1);
					return (WCombState);
				    }
			    }
		    }
		return null;
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return null;
    }
    
    public static int OtkUtilCombinedStateInfo (WSolid WSolid, CombState WCombState, BufferedWriter InfoFile)
    {
	try
	    {	
		String file_buff; 
		
		if(WCombState == null)
		    return -1;
		
		CombStateData WCombStateData = WCombState.GetCombStateData();
		
		InfoFile.write ("===================================");
		InfoFile.newLine();
		InfoFile.write("Combined State Info:" );
		InfoFile.newLine();
		InfoFile.write ("===================================");
		InfoFile.newLine();
		
		InfoFile.newLine();
		InfoFile.write(" - CombState data:");
		InfoFile.newLine();
		String  CombStateName = WCombStateData.GetCombStateName();
		file_buff = "\t - CombState name: " + CombStateName;
		InfoFile.write(file_buff);
		InfoFile.newLine();
		
		boolean isexploded = WCombStateData.GetIsExploded();
		if(isexploded == true)
		    {
			InfoFile.write("\t - IsExploded: TRUE");
			InfoFile.newLine();
		    }
		
		else if(isexploded == false)
		    {
			InfoFile.write("\t - IsExploded: FALSE");
			InfoFile.newLine();
		    }
		
		CrossSectionClipOption clipOption = WCombStateData.GetClipOption();
		if(clipOption == CrossSectionClipOption.VIS_OPT_NONE)
		    InfoFile.write("\t - ClipOption: VIS_OPT_NONE");
		else if(clipOption == CrossSectionClipOption.VIS_OPT_FRONT)
		    InfoFile.write("\t - ClipOption: VIS_OPT_FRONT");
		else if(clipOption == CrossSectionClipOption.VIS_OPT_BACK)
		    InfoFile.write("\t - ClipOption: VIS_OPT_BACK");
		
		InfoFile.newLine();
		
		CombState WActiveCombState = WSolid.GetActiveCombState();
		if(WActiveCombState != null)
		    {
			CombStateData ActiveCombStateData = WActiveCombState.GetCombStateData();
			if(ActiveCombStateData != null)
			    {
				String  ActiveCombStateName = ActiveCombStateData.GetCombStateName();
				if(ActiveCombStateName.length() != 0 && ActiveCombStateName != null)
				    file_buff = "\t - Active CombState name: " + ActiveCombStateName;
				
				InfoFile.write(file_buff);
				InfoFile.newLine();
			    }
		    }
		
		ModelItems ReferenceStates = WCombStateData.GetReferences ();
		int ReferencesSize = ReferenceStates.getarraysize();
		file_buff = "\t - Number of CombState References: " + ReferencesSize + "\n";
		
		InfoFile.write(file_buff);
		InfoFile.newLine();
		
		for(int i = 0; i < ReferencesSize; i++)
		    {
			ModelItem RefItem = ReferenceStates.get(i);
			ModelItemType type = RefItem.GetType();
			switch(type.getValue())
			    {
			    case ModelItemType._ITEM_SIMPREP:
				OtkUtilSimpRepInfo((WSimpRep)(RefItem), InfoFile);
				break;
				
			    case ModelItemType._ITEM_EXPLODED_STATE:
				OtkUtilExplodedStateInfo((WExplodedState)(RefItem), InfoFile);
				break;
				
			    case ModelItemType._ITEM_LAYER_STATE:
				otkLayerStateUtils.otkUtilLayerStateInfo(WSolid, (LayerState)(RefItem), InfoFile);
				break;
				
			    case ModelItemType._ITEM_STYLE_STATE:
				OtkUtilStyleStateInfo(WSolid, (StyleState)(RefItem), InfoFile);
				break;
			    }
		    }
		return 0;
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return (0);
    }
    
    public static int OtkUtilSimpRepInfo(WSimpRep WSimpRep, BufferedWriter InfoFile)
    {
	String file_buff; 
	
	if(WSimpRep == null)
	    return -1;
	try
	    {	
		SimpRepInstructions SimpRepInstructions = WSimpRep.GetInstructions();
		
		InfoFile.write ("===================================");
		InfoFile.newLine();
		InfoFile.write("- SimpRep Info -" );
		InfoFile.newLine();
		InfoFile.write ("===================================");
		InfoFile.newLine();
		
		boolean istemp = SimpRepInstructions.GetIsTemporary();
		if(istemp == true)
		    {
			InfoFile.write("\t - Temp value: TRUE");
			InfoFile.newLine();
			
			RetrieveExistingSimpRepInstructions CreateInstr =	(RetrieveExistingSimpRepInstructions) (SimpRepInstructions);
			
			String  SimpRepName = CreateInstr.GetExistSimpName();
			file_buff = "\t - SimpRep name: " + SimpRepName;
			InfoFile.write(file_buff);
			InfoFile.newLine();
			
		    }
		else
		    {
			InfoFile.write("\t - Temp value: FALSE");
			InfoFile.newLine();
			
			CreateNewSimpRepInstructions CreateInstr =	(CreateNewSimpRepInstructions) (SimpRepInstructions);
			
			String  SimpRepName = CreateInstr.GetNewSimpName();
			file_buff = "\t - SimpRep name: " + SimpRepName;
			InfoFile.write(file_buff);
			InfoFile.newLine();
		    }
		SimpRepActionType WActionType = SimpRepInstructions.GetDefaultAction();
		file_buff = "\t - SimpRep Default Action: " + WActionType.getValue();
		InfoFile.write(file_buff);
		InfoFile.newLine();
		
		boolean isdefault = WSimpRep.IsSimpRepInstructionDefault();
		if(isdefault == true)
		    {
			InfoFile.write("\t - Is Default Action Type: TRUE");
			InfoFile.newLine();
		    }
		else if(isdefault == false)
		    {
			InfoFile.write("\t - Is Default Action Type: FALSE");
			InfoFile.newLine();
		    }
		
		
		boolean tmpval = WSimpRep.GetSimprepdataTempvalue();
		if(tmpval == true)
		    {
			InfoFile.write("\t - SimprepdataTempvalue: TRUE");
			InfoFile.newLine();
		    }
		else
		    {
			InfoFile.write("\t - SimprepdataTempvalue: FALSE");
			InfoFile.newLine();
		    }
		
		SimpRepItems  pSimpRepItems = SimpRepInstructions.GetItems();
		
		int SimpRepItemsSize = pSimpRepItems.getarraysize();
		file_buff = "\t - Number of Actions in SimpRep: " + SimpRepItemsSize;
		InfoFile.write(file_buff);
		InfoFile.newLine();
		
		for (int ii = 0; ii < SimpRepItemsSize; ii++)
		    {
			SimpRepItem  psimpItem = pSimpRepItems.get(ii);
			SimpRepAction   psimpRepAction = psimpItem.GetAction();
			if(psimpRepAction != null)
			    {
				SimpRepActionType actType =  psimpRepAction.GetType();
				file_buff = "\t - SimpRep Action Type: " + actType.getValue();
				InfoFile.write(file_buff);
				InfoFile.newLine();
				
				if(actType == SimpRepActionType.SIMPREP_SUBSTITUTE)
				    {
					SimpRepItemPath  psimpItemPath = psimpItem.GetItemPath();
					
					String SubstName = WSimpRep.GetSimprepSubstitutionName(psimpItemPath);
					file_buff = "\t - SimpRep SubstitutionName: " + SubstName;
					InfoFile.write(file_buff);
					InfoFile.newLine();
				    }
			    }
		    }
		return 0;
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return 0;
    }
    
    public static int OtkUtilExplodedStateInfo(WExplodedState wExpldState, BufferedWriter InfoFile)
    {
	String file_buff; 
	
	if(wExpldState == null)
	    return -1;
	
	try
	    {
		String ExpldStateName = wExpldState.GetExplodedStateName();
		
		InfoFile.write ("===================================");
		InfoFile.newLine();
		InfoFile.write("- Exploded State Info -" );
		InfoFile.newLine();
		InfoFile.write ("===================================");
		InfoFile.newLine();
		
		file_buff = "\t - ExplodedState name: " + ExpldStateName;
		InfoFile.write(file_buff);
		InfoFile.newLine();
		
		ExplodedAnimationMoveInstructions wExpldStateMvInstructions = wExpldState.GetExplodedStateMoves(); 
		
		int arraySize = wExpldStateMvInstructions.getarraysize();
		file_buff = "\t - Exploded Animation MoveInstructions array size: " + arraySize;
		InfoFile.write(file_buff);
		InfoFile.newLine();
		
		for (int i = 0; i < arraySize; i++)
		    {
			file_buff = "\t - Exploded Animation MoveInstruction: " + (i+1) + " info";
			InfoFile.write(file_buff);
			InfoFile.newLine();	
			
			ExplodedAnimationMoveInstruction MoveInstruction = wExpldStateMvInstructions.get(i);
			ExplodedAnimationMove WExpldAnimMove = MoveInstruction.GetMove ();
			
			WComponentPaths WCompPaths = MoveInstruction.GetCompSet ();
			
			int CompSetarraySize1 = WCompPaths.getarraysize();
			file_buff = "\t - CompSet array size: " + CompSetarraySize1;
			InfoFile.write(file_buff);
			InfoFile.newLine();
			
			ExplodedAnimationMoveType wMoveType = WExpldAnimMove.GetMoveType();
			file_buff = "\t - ExplodedState Move Type: " + wMoveType.getValue();
			InfoFile.write(file_buff);
			InfoFile.newLine();
			
			Point3D WStartPt = WExpldAnimMove.GetStartPoint();
			file_buff = "\t - StartPoint: [" + WStartPt.get(0) + ", " + WStartPt.get(1) + ", " + WStartPt.get(2) + "] ";
			InfoFile.write(file_buff);
			InfoFile.newLine();
			
			Vector3D WDirVector = WExpldAnimMove.GetDirVector();
			file_buff = "\t - DirVector: [" + WDirVector.get(0) + ", " + WDirVector.get(1) + ", " + WDirVector.get(2) + "] ";
			InfoFile.write(file_buff);
			InfoFile.newLine();
			
			double WValue = WExpldAnimMove.GetValue();
			file_buff = "\t - ExplodedState Move: " + WValue;
			InfoFile.write(file_buff);
			InfoFile.newLine();
		    }
		WComponentPaths wCompPaths = wExpldState.GetExplodedcomponents();
		int arraySize1 = wCompPaths.getarraysize();
		file_buff = "\t - Exploded components array size: " + arraySize1;
		InfoFile.write(file_buff);
		InfoFile.newLine();
		
		return 0;
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return 0;
    }
    
	//Asks user to enter number and retuns that element from pfcModelItems array
    public static ModelItem otkUtilMdlItemSelect(ModelItems pModelItems, String itemtype, boolean IsRegTest)
    {
	try
	    {
		Session session = pfcSession.GetCurrentSession();
		String buff;
		String line;
		stringseq msgs = stringseq.create();
		
		line = itemtype;
		line += ": ";
		Integer index;
		int arraySize = pModelItems.getarraysize ();
		if(arraySize <= 0)
		    return null;
		
			// for reg test
		if(IsRegTest == true)
		    index = new Integer(1);
		
		// For OKT example: Asks user to give I/P from UI
		else
		    {
			for (int i = 0; i < arraySize; i++)
			    {
				ModelItem pModelItem = pModelItems.get(i);
				if(pModelItem != null)
				    {
					if(i+1 < arraySize)
					    buff = " " + (i+1) + ": " + pModelItem.GetName();
					else
					    buff = " " + (i+1) + ": " + pModelItem.GetName();
					line += buff;
				    }
			    }
			msgs.append(line);
			session.UIDisplayMessage("combstate_msg.txt", "USER Enter number to select %0s", msgs);
			index = session.UIReadIntMessage(1, arraySize);
		    }
		if(index.intValue() <= 0 || index.intValue() > arraySize)
		    return null;
		
		return(pModelItems.get(index.intValue()-1));
	    }
	catch (Exception e)
		{
		    System.out.println("1: " + e);
		}
		return null;
    }
    
    public static int OtkUtilStyleStateInfo(WSolid WSolid, StyleState WstyleState, BufferedWriter InfoFile)
    {
	String file_buff; 
	
	if(WstyleState == null)
	    return -1;
	try
	    {
		String StyleStateName =  WstyleState.GetName();
		
		InfoFile.write ("===================================");
		InfoFile.newLine();
		InfoFile.write("- StyleState State Info -" );
		InfoFile.newLine();
		InfoFile.write ("===================================");
		InfoFile.newLine();
			
		file_buff = "\t - StyleState Name: " + StyleStateName;
		InfoFile.write(file_buff);
		InfoFile.newLine();
		
		int StyleStateId = WstyleState.GetId();
		if(StyleStateId > 0)
		    {
			file_buff = "\t - StyleState Id: " + StyleStateId;
			InfoFile.write(file_buff);
			InfoFile.newLine();
		    }
		boolean isdefault = WstyleState.IsStyleStateDefault();
		
		if(isdefault == true)
		    {
			InfoFile.write("\t - StyleState is default ");
			InfoFile.newLine();
		    }
		else
		    {
			InfoFile.write("\t - StyleState is not default ");
			InfoFile.newLine();
		    }
		
		StyleState ActiveWstyleState = WSolid.GetActiveStyleState();
		
		if(ActiveWstyleState != null)
		    {
			file_buff = "\t - Active StyleState Name: " + ActiveWstyleState.GetName();
			InfoFile.write(file_buff);
			InfoFile.newLine();
		    }
		return 0;
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return 0;
    }
    
    //Utility to create new combined state
    public static CombState otkUtilCreateCombinedState (WSolid myWSolid, String NewCombStateName, boolean IsRegTest, BufferedWriter fp_out_combstate )
    {
	try
	    {
		String buff;
		ModelType modelType = myWSolid.GetType();
		Session session = pfcSession.GetCurrentSession ();
		
		if(NewCombStateName.length() != 0 && NewCombStateName != null && (modelType == ModelType.MDL_ASSEMBLY || modelType == ModelType.MDL_PART))
		    {
			ModelItems MdlItems = ModelItems.create();
			
			// Adds simprep item
			ModelItems WSimpReps = myWSolid.ListItems(ModelItemType.ITEM_SIMPREP);
			ModelItem pMdlItem = otkUtilMdlItemSelect(WSimpReps, "SimpRep", IsRegTest);
			if(pMdlItem != null)
			    {
				MdlItems.append(pMdlItem);
				fp_out_combstate.write("Selected Item Type: " + pMdlItem.GetType());
				fp_out_combstate.write(", ID: " + pMdlItem.GetId());
				fp_out_combstate.newLine();
			    }
			
			// Code to add Style State and Exploded State items, applicable for assemblies only
			if(modelType == ModelType.MDL_ASSEMBLY)
			    {
				// Adds Style State item
				StyleStates WStyleStates =   myWSolid.ListStyleStateItems();
				int StyleStatesArraySize = WStyleStates.getarraysize ();
				if(StyleStatesArraySize > 0)
				    {
					Integer SelectedstyleSateNo;
					// For RegTest
					if(IsRegTest == true)
					    {
						SelectedstyleSateNo = new Integer(1);
					    }
					// For OKT example: Asks user to give I/P from UI
					else
					    {
						String line ;
						stringseq msgs = stringseq.create();
						line = "Style State: "; 
						for (int i = 0; i < StyleStatesArraySize; i++)
						    {
							StyleState WStyleState = WStyleStates.get(i);
							if(WStyleState != null)
							    {
								if(i+1 < StyleStatesArraySize)
								    buff = " " + (i+1) + ": " +  WStyleState.GetName();
								
								else
								    buff = " " + (i+1) + ": " +  WStyleState.GetName();
								
								line += buff;
								}
						    }
						msgs.set(0, line);
						session.UIDisplayMessage("combstate_msg.txt", "USER Enter number to select %0s", msgs);
						SelectedstyleSateNo = session.UIReadIntMessage(1, StyleStatesArraySize);
					    }
					if(SelectedstyleSateNo.intValue() > 0 && SelectedstyleSateNo.intValue()  <= StyleStatesArraySize)
					    {
						pMdlItem = (ModelItem)(WStyleStates.get(SelectedstyleSateNo.intValue()-1));
						MdlItems.append(pMdlItem);
						fp_out_combstate.write("Selected Item Type: " + pMdlItem.GetType());
						fp_out_combstate.write(", ID: " + pMdlItem.GetId());
						fp_out_combstate.newLine();
					    }
				    }


				//Adds Exploded State item
				ModelItems WExplodedStates = myWSolid.ListItems(ModelItemType.ITEM_EXPLODED_STATE);
				pMdlItem = otkUtilMdlItemSelect(WExplodedStates, "Exploded State", IsRegTest);
				if( pMdlItem != null)
				    {
					MdlItems.append( pMdlItem);
					fp_out_combstate.write("Selected Item Type: " + pMdlItem.GetType());
					fp_out_combstate.write(", ID: " + pMdlItem.GetId());
					fp_out_combstate.newLine();
				    }
			    }

			//Adds Cross Section item
			XSections WXSections = myWSolid.ListCrossSections();
			int XSectionsArraySize = WXSections.getarraysize ();
			if(XSectionsArraySize > 0)
			    {
				Integer XSectionNo;
				// For RegTest
				if(IsRegTest == true)
				    {
					XSectionNo = new Integer(1);
				    }
				// For OKT example: Asks user to give I/P from UI
				else
				    {
					String line;
					stringseq msgs = stringseq.create();
					line = "Cross section: ";
					for (int i = 0; i < XSectionsArraySize; i++)
					    {
						XSection WXSection = WXSections.get(i);
						if(i+1 < XSectionsArraySize)
						    buff = " " + (i+1) + ": " + WXSection.GetName();
						else
						    buff = " " + (i+1) + ": " + WXSection.GetName();
						
						line += buff;
					    }
					msgs.append(line);
					session.UIDisplayMessage("combstate_msg.txt", "USER Enter number to select %0s",msgs);
					XSectionNo = session.UIReadIntMessage(1, XSectionsArraySize);
				    }
				if(XSectionNo.intValue()  > 0 && XSectionNo.intValue()  <= XSectionsArraySize)
				    {
					pMdlItem = (ModelItem)(WXSections.get(XSectionNo.intValue()-1));
					MdlItems.append(pMdlItem);
					fp_out_combstate.write("Selected Item Type: " + pMdlItem.GetType());
					fp_out_combstate.write(", ID: " + pMdlItem.GetId());
					fp_out_combstate.newLine();
				    }
			    }
			
			//Adds Layer State item
			String newLayerSatetName;
			newLayerSatetName = NewCombStateName + "NewLayerState";
			LayerState WLayerState = otkLayerStateUtils.OTKUtilCreateLayerState (myWSolid, "NewLayerState");
			if(WLayerState != null)
			    {
				MdlItems.append((ModelItem)(WLayerState));
			    }
			
			// Creates new combined state
			CombStateData WCombStateData =  wfcCombState.CombStateData_Create(MdlItems,  CrossSectionClipOption.VIS_OPT_FRONT, false);
			CombState WCombState = myWSolid.CreateCombState(NewCombStateName, WCombStateData);
			
			if(WCombState != null)
			    myWSolid.ActivateCombState(WCombState);
			
			return (WCombState);
		    }
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return null;
    }
    
};





